package dailyHumor;

import java.io.File;
import java.io.FileOutputStream;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class DailyHumor {
	private File humor;
	private File gtd;
	private SAXReader reader = new SAXReader();
	
	public void init(){
		String s = DailyHumor.class.getResource("").getPath();
		humor=new File(s+"humor.xml");
		gtd=new File(s+"humorGTD.xml");
	}
	public void getHumor() throws Exception{
		init();
		
		Document doc = reader.read(gtd);
		Element root =doc.getRootElement();
		Attribute nextH=root.attribute("next");
		int next=Integer.parseInt(nextH.getValue());
		Attribute maxH=root.attribute("max");
		int max=Integer.parseInt(maxH.getValue());
		
		Document doc1=reader.read(humor);
		Element root1=doc1.getRootElement();
		Node thisH=root1.node(next*2-1);
		String humor=thisH.getText();
		System.out.println(humor);
		
		next++;
		if(next>max){
			next=1;
		}
		nextH.setValue(next+"");
		OutputFormat f = OutputFormat.createPrettyPrint();
		XMLWriter writer = new XMLWriter(new FileOutputStream(gtd),f);
		writer.write(doc);
	}
	public static void main(String[] args) throws Exception {
		new DailyHumor().getHumor();
	}
}
